<template>
  <BaseDialog
    :title="dialogTitle"
    width="60"
    :dialog-show.sync="dialogShow"
    footer
    @submit="saveOthers"
    @cancel="close"
  >
    <el-form :model="othersForm" label-position="top" class="others-form">
      <!-- Suwork配置 -->
      <div v-if="dialogTitle == $t('Suwork配置')">
        <el-form-item
          label="Suwork API"
          :prop="
            othersForm.baidu_others_account ||
            othersForm.baidu_others_password ||
            othersForm.baidu_others_token
              ? 'baidu_others_password'
              : ' '
          "
        >
          <el-input
            v-model="othersForm.suwork_api"
            class="right"
            :placeholder="$t('请配置接口地址')"
            type="text"
          />
        </el-form-item>
        <el-form-item
          label="Suwork APPID"
          :prop="
            othersForm.baidu_others_account ||
            othersForm.baidu_others_password ||
            othersForm.baidu_others_token
              ? 'baidu_others_account'
              : ' '
          "
        >
          <el-input
            v-model="othersForm.suwork_appid"
            class="left"
            :placeholder="$t('请输入appid')"
            type="password"
          />
        </el-form-item>
        <el-form-item
          :label="$t('Suwork密钥')"
          :prop="
            othersForm.baidu_others_account ||
            othersForm.baidu_others_password ||
            othersForm.baidu_others_token
              ? 'baidu_others_password'
              : ' '
          "
        >
          <el-input
            v-model="othersForm.suwork_secret"
            class="right"
            :placeholder="$t('请输入密钥')"
            type="password"
          />
        </el-form-item>
      </div>
      <!-- 百度翻译配置 -->
      <div v-if="dialogTitle == $t('百度翻译配置')">
        <el-form-item :label="$t('百度翻译APPID')">
          <el-input
            v-model="othersForm.baidu_translate_appid"
            type="password"
          />
        </el-form-item>
        <el-form-item :label="$t('百度翻译Token')">
          <el-input
            v-model="othersForm.baidu_translate_token"
            type="password"
          />
        </el-form-item>
      </div>
      <!-- sitemap密钥 -->
      <div v-if="dialogTitle == $t('SitemMap配置')">
        <el-form-item :label="$t('百度密钥')">
          <el-input v-model="othersForm.baidu_sitemap_token" type="password" />
        </el-form-item>
      </div>
      <!-- 关键词排名查询配置 -->
      <div v-if="dialogTitle == $t('关键词排名查询配置')">
        <el-form-item :label="$t('自动查询频率')">
          <el-select v-model="othersForm.keywords_search_time">
            <el-option :label="$t('每小时')" value="hour" />
            <el-option :label="$t('每天')" value="day" />
            <el-option :label="$t('每周')" value="week" />
          </el-select>
        </el-form-item>
        <el-form-item label="站长之家 Token">
          <el-input v-model="othersForm.chinaz_token" type="password" />
        </el-form-item>
      </div>
      <!-- 词爪配置 -->
      <div v-if="dialogTitle == $t('词爪配置')">
        <el-form-item :label="$t('词爪密钥')" :prop="othersForm.cizhua_secret">
          <el-input
            v-model="othersForm.cizhua_secret"
            class="right"
            :placeholder="$t('请输入词爪密钥')"
            type="password"
          />
        </el-form-item>
      </div>
      <!-- 阿里云短信配置 -->
      <div v-if="dialogTitle == $t('阿里短信')">
        <el-form-item label="AccessKeyid" :prop="othersForm.ali_access_key_id">
          <el-input
            v-model="othersForm.ali_access_key_id"
            class="right"
            :placeholder="$t('请输入阿里短信AccessKeyid')"
          />
        </el-form-item>
        <el-form-item
          label="AccessKeySecret"
          :prop="othersForm.ali_access_key_secret"
        >
          <el-input
            v-model="othersForm.ali_access_key_secret"
            class="right"
            :placeholder="$t('请输入阿里短信AccessKeySecret')"
          />
        </el-form-item>
        <el-form-item
          :label="$t('模板Code')"
          :prop="othersForm.ali_template_code"
        >
          <el-input
            v-model="othersForm.ali_template_code"
            class="right"
            :placeholder="$t('请输入阿里短信模板Code')"
          />
        </el-form-item>
        <el-form-item :label="$t('签名名称')" :prop="othersForm.ali_sign_name">
          <el-input
            v-model="othersForm.ali_sign_name"
            class="right"
            :placeholder="$t('请输入阿里短信签名名称')"
          />
        </el-form-item>
      </div>
    </el-form>
  </BaseDialog>
</template>
<script>
import systemApi from '@/api/system/system.js'
export default {
  props: ['title', 'dialog'],
  data() {
    return {
      dialogShow: false,
      othersForm: {
        group: 'others',
        suwork_api: '',
        suwork_appid: '',
        suwork_secret: '',
        keywords_search_time: '',
        five_baidu_pc: '',
        five_baidu_mob: '',
        haosou_pc: '',
        sougou_mob: '',
        baidu_translate_appid: '',
        baidu_translate_token: '',
        baidu_sitemap_token: '',
        google_sitemap_token: '',
        three_sitemap_token: '',
        sogou_sitemap_token: '',
        cizhua_appid: '',
        cizhua_secret: '',
        chinaz_token: '',
        ali_access_key_id: '',
        ali_access_key_secret: '',
        ali_template_code: '',
        ali_sign_name: ''
      },
      dialogTitle: ''
    }
  },
  watch: {
    dialog: {
      handler(data) {
        this.dialogShow = data
      },
      immediate: true
    },
    title: {
      handler(data) {
        if (data) {
          this.dialogTitle = data
        }
      },
      immediate: true
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 配置项列表
    getList() {
      systemApi.sysList().then(res => {
        if (res.code == 0) {
          const othersData = res.data[3].children
          othersData.forEach(el => {
            if (el.children.length > 0) {
              el.children.forEach(item => {
                if (item.children.length > 0) {
                  item.children.forEach(element => {
                    this.othersForm[element.title] = element.value
                  })
                } else {
                  this.othersForm[item.title] = item.value
                }
              })
            } else if (el.children.length == 0) {
              this.othersForm[el.title] = el.value
            }
          })
        }
      })
    },
    saveOthers() {
      systemApi.sysUpdate({ ...this.othersForm }).then(res => {
        if (res.code == 0) {
          this.$message.success(res.msg)
          this.close()
        }
      })
    },
    close() {
      this.$emit('update:dialog', false)
      this.dialogShow = false
    }
  }
}
</script>
<style scoped lang="scss">
.others-form {
  .el-select {
    width: 100%;
  }
}
</style>
